package system

import (
	"gin-admin/common/dto/request"
	"gin-admin/common/dto/response"
	"gin-admin/core"
	serviceRequest "gin-admin/service/dto/request"
	"gin-admin/service/v1/system"

	"github.com/gin-gonic/gin"
)

type MenuHandler struct {
	MenuService system.MenuService
}

var MenuHandlerApp = &MenuHandler{
	MenuService: *system.MenuServiceApp,
}

// @Summary 创建菜单
// @Tags /v1/菜单管理
// @Accept json
// @Product json
// @Param data body request.CreateMenuRequest true "创建菜单请求参数"
// @Router /api/v1/menu/create [post]
func (mh *MenuHandler) CreateMenuHandler(ctx *gin.Context) {
	var createMenuRequest serviceRequest.CreateMenuRequest
	if !core.ValidateParams(ctx, &createMenuRequest) {
		response.FailedWithCode(ctx, response.MenuParameterErrorCode)
		return
	}
	if err := mh.MenuService.CreateMenuService(&createMenuRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}

	response.SuccessNoData(ctx)
}

// @Summary 删除菜单
// @Tags /v1/菜单管理
// @Accept json
// @Product json
// @Param id body request.IDRequest true "删除菜单请求id参数"
// @Router /api/v1/menu/delete [delete]
func (mh *MenuHandler) DeleteMenuHandler(ctx *gin.Context) {
	var iDRequest request.IDRequest
	if !core.ValidateParams(ctx, &iDRequest) {
		response.FailedWithCode(ctx, response.MenuParameterErrorCode)
		return
	}
	if err := mh.MenuService.DeleteMenuService(&iDRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 修改菜单
// @Tags /v1/菜单管理
// @Accept json
// @Product json
// @Param data body request.ModifyMenuRequest true "修改菜单请求参数"
// @Router /api/v1/menu/modify [put]
func (mh *MenuHandler) ModifyMenuHandler(ctx *gin.Context) {
	var modifyMenuRequest serviceRequest.ModifyMenuRequest
	if !core.ValidateParams(ctx, &modifyMenuRequest) {
		return
	}
	if err := mh.MenuService.ModifyMenuService(&modifyMenuRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 获取菜单信息
// @Tags /v1/菜单管理
// @Accept json
// @Product json
// @Param id path string true "查询菜单详情请求参数"
// @Router /api/v1/menu/info/{id} [get]
func (mh *MenuHandler) GetMenuInfoHandler(ctx *gin.Context) {
	id := ctx.Param("id")
	if !core.ValidateParams(ctx, id) {
		return
	}
	result, err := mh.MenuService.GetMenuInfoService(id)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, result)
}

// @Summary 获取菜单列表
// @Tags /v1/菜单管理
// @Accept json
// @Product json
// @Param data query request.BaseQueryRequest true "查询列表请求参数"
// @Router /api/v1/menu/list [get]
func (mh *MenuHandler) GetMenuListHandler(ctx *gin.Context) {
	var queryRequest request.BaseQueryRequest

	if !core.ValidateParams(ctx, &queryRequest) {
		return
	}
	result, err := mh.MenuService.GetMenuListService(&queryRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, result)
}
